define(['common/js/filter/boss-filter-0.1','common/lib/emailtip/jquery.emailtip.js'],function(){
	var formTip = {
		default:{
			"required":"该字段不能为空",
			"minlength":"该字段长度不能小于{{minlength}}",
			"maxlength":"该字段长度不能大于{{maxlength}}",
			"pattern":"您输入的格式有误"
		}
	};

	diretiveModule.directive('plusDynamicForm', function($compile) {
		var formName = "plusDynamicForm";
		for(var i=0;i<6;i++){
			formName += Math.ceil(Math.random()*10);
		}

		return {
			scope : false,
			restrict : "E",
			replace : true,
			transclude : true,
			controller : function($scope,$injector){
				$scope.formName = formName;
				$scope.isCascading = false; //级联下拉框是否正在加载

				//表单属性
				$scope.formNames = [];
				$scope.formValidation = {};
				$scope.defaultValidation = {};
				$scope.validStrs = ["required","minlength","maxlength","pattern"];

				$scope.myData = [];
				$scope.cleanNames = [];//使用“Form setPristine”清空的条件项
				$scope.refreshNames = [];//使用“广播”清空的条件项
				$scope.searchNames = [];//用户作为搜索条件
				$scope.searchConditions = {};

				$scope.confirmDoIt = function(){
					//表单验证
					for(var index in $scope.formNames){
						var name = $scope.formNames[index];
						var nameObj = $scope[formName][name];
						var nameValidation = $scope.defaultValidation[name];
						for(var key in nameValidation){
							if(nameObj.$error[key]){
								$scope.errorMsg = nameValidation[key];
								return;
							}
						}
					}

					// 验证通过
					var emptyForm = true;
					if($scope.confirmDoStr){
						var condition = angular.copy($scope.searchConditions);
						emptyForm = $scope[$scope.confirmDoStr](condition);
					}

					if(emptyForm){
						emptySearchConditions();
					}
				};

				$scope.cancelDoIt = function(){
					console.log("= = = = =  =关闭Modal");
					emptySearchConditions();

					//点击取消之后的额外操作
					if($scope.cancelDoStr){
						var condition = angular.copy($scope.searchConditions);
						$scope[$scope.cancelDoStr](condition);
					}
				};

				$scope.extendDoIt = function(){
					//表单验证
					for(var index in $scope.formNames){
						var name = $scope.formNames[index];
						var nameObj = $scope[formName][name];
						var nameValidation = $scope.defaultValidation[name];
						for(var key in nameValidation){
							if(nameObj.$error[key]){
								$scope.errorMsg = nameValidation[key];
								return;
							}
						}
					}

					// 验证通过
					var emptyForm = true;
					if($scope.extendDoStr){
						var condition = angular.copy($scope.searchConditions);
						emptyForm = $scope[$scope.extendDoStr](condition);
					}

					if(emptyForm){
						emptySearchConditions();
					}
				};

				$scope.emptySearchConditions = emptySearchConditions;

				function emptySearchConditions(){
					$scope.errorMsg = "";

					// 通过表单处理清空
					for(var key in $scope.cleanNames){
						var name = $scope.cleanNames[key];
						$scope.searchConditions[name] = "";
						$scope[formName][name].$setPristine();
					}

					//通过事件广播清空
					for(var index2 in $scope.refreshNames){
						$scope.$broadcast("refresh-"+$scope.refreshNames[index2]);
					}
				}

			},
			template : function(elm,attrs){
				var confirmLabel = attrs.confirmLabel?attrs.confirmLabel:"确认";
				var cancelLabel = attrs.cancelLabel?attrs.cancelLabel:"取消";
				if(attrs['extendDo']){
					var extendLabel = attrs.extendLabel?attrs.extendLabel:"扩展";
					return '<form novalidate class="form-horizontal" role="form" name="'+formName+'" style="margin-bottom:0;">' +
						'<tmp ng-transclude></tmp>' +
						'<div class="form-group"> ' +
						'<div class="col-sm-offset-3 col-sm-9" ng-show="errorMsg">' +
						'<span class="text-lg text-danger" >{{errorMsg}}</span>' +
						'</div> ' +
						'</div>' +
						'<div class="form-group modal-footer" style="margin-bottom:0;"> ' +
						'<div class="col-sm-offset-3 col-sm-9">' +
						'<button type="submit" class="btn btn-primary" ng-disabled="isCascading" ng-click="confirmDoIt()"  aria-hidden="true">'+confirmLabel+'</button> ' +
						'<button type="submit" class="btn btn-primary" ng-disabled="isCascading" ng-click="extendDoIt()"  aria-hidden="true">'+extendLabel+'</button> ' +
						'<button class="btn btn-primary" ng-click="cancelDoIt()" aria-hidden="true"  data-dismiss="modal">'+cancelLabel+'</button>' +
						'</div> ' +
						'</div>' +
						'</form>'
				}
				else{
					return '<form novalidate class="form-horizontal" role="form" name="'+formName+'" style="margin-bottom:0;">' +
					'<tmp ng-transclude></tmp>' +
					'<div class="form-group"> ' +
					'<div class="col-sm-offset-3 col-sm-9" ng-show="errorMsg">' +
					'<span class="text-lg text-danger" >{{errorMsg}}</span>' +
					'</div> ' +
					'</div>' +
					'<div class="form-group modal-footer" style="margin-bottom:0;"> ' +
					'<div class="col-sm-offset-3 col-sm-9">' +
					'<button type="submit" class="btn btn-primary" ng-disabled="isCascading" ng-click="confirmDoIt()"  aria-hidden="true">'+confirmLabel+'</button> ' +
					'<button class="btn btn-primary" ng-click="cancelDoIt()" aria-hidden="true"  data-dismiss="modal">'+cancelLabel+'</button>' +
					'</div> ' +
					'</div>' +
					'</form>'
				}

			},
			compile : function(tElm,tAttrs){
				return {
					pre : function(scope, elm, attrs) {
						scope.confirmDoStr = attrs.confirmDo;
						scope.cancelDoStr = attrs.cancelDo;
						scope.extendDoStr = attrs.extendDo;

						if(attrs.formValidation){
							scope.formValidation = scope[attrs.formValidation];
						}

						//添加表单清空事件监听
						if(attrs.formName){
							scope.$on("clearForm-"+attrs.formName,function(){
								scope.emptySearchConditions();
							});
						}

						//添加错误信息更新操作
						if(attrs.selfError){
							scope.$on("setFormMsg-"+attrs.formName,function(event,msg){
								scope.errorMsg = msg;
							});
						}

						//添加表单回显监听
						if(attrs.echo){
							scope.echo = true;

							scope.$on("echoForm-"+attrs.formName,function(event,echoValue){
								if(!angular.isObject(echoValue)){
									console.log("回显数据格式有误，必须是json格式,key为form'搜索'字段");
									return;
								}

								//更改搜索条件数据
								for(var ii in scope.searchNames){
									var echoV = echoValue[scope.searchNames[ii]];
									if(echoV != undefined){
										scope.searchConditions[scope.searchNames[ii]] = echoV;
									}
								}

								//更改显示
								//通过事件广播更改（无法通过双向绑定更改）
								for(var index2 in scope.refreshNames){
									var echoOptionName = scope.refreshNames[index2];
									if(echoValue[echoOptionName]){
										scope.$broadcast("echo-"+echoOptionName,echoValue[echoOptionName]);
									}
								}
							});
						}
					}
				};
			}
		};
	});

	diretiveModule.directive('formOptionHide', function() {
		return {
			scope : true,
			restrict : "E",
			replace : false,
			require: '^plusDynamicForm',
			link : function(scope, elm, attrs) {
				scope.searchNames.push(attrs.optionName);
				scope.cleanNames.push(attrs.optionName);
				scope.searchConditions[attrs.optionName] = attrs.optionValue;
			},
			template : function(elm,attrs){
				return '<input type="hidden" name="'+attrs.optionName+'" ng-model="searchConditions.'+attrs.optionName+'" />';
			}
		};
	});


    diretiveModule.directive('formOptionStatic', function() {
        return {
            scope : {
                optionValue:'@'
            },
            restrict : "E",
            replace : true,
            require: '^plusDynamicForm',
            template : function(elm,attrs){
                return '<div class="form-group"> <label class="col-sm-3 control-label">'+attrs.optionLabel+'</label> ' +
                    '<div class="col-sm-8"> <div class="form-control-static" ng-bind-html="optionValue"></div></div> </div>';
            }
        };
    });

    diretiveModule.directive('formOptionTextarea', function() {
		return {
			scope : true,
			restrict : "E",
			replace : true,
			require: '^plusDynamicForm',
			template : function(elm,attrs){
				//检查校验属性
				var validationStr = "";
				if(typeof attrs.required != "undefined"){
					validationStr += " required";
				}
				if(attrs.minlength){
					validationStr += ' ng-minlength="'+attrs.minlength+'"';
				}
				if(attrs.maxlength){
					validationStr += ' ng-maxlength="'+attrs.maxlength+'"';
				}
				if(attrs.pattern){
					validationStr += ' ng-pattern="'+attrs.pattern+'"';
				}

				return '<div class="form-group"> <label class="col-sm-3 control-label">'+attrs.optionLabel+'</label> ' +
						'<div class="col-sm-8"> <textarea name="'+attrs.optionName+'" ng-model="'+"searchConditions."+attrs.optionName+'" class="form-control" '+validationStr+' placeholder="'+attrs.optionTip+'"></textarea> </div> </div>';
			},
			link : function(scope, elm, attrs) {
				var optionName = attrs.optionName;
				scope.searchNames.push(optionName);
				scope.cleanNames.push(optionName);

				scope.formNames.push(optionName);
				scope.defaultValidation[optionName] = {};

				angular.forEach(scope.validStrs,function(validationStr,index){
					if(typeof attrs[validationStr] != undefined){
						var tip;
						if(scope.formValidation[optionName]){
							tip = scope.formValidation[optionName][validationStr];
						}else{
							tip = formTip.default[validationStr];
							if(validationStr == "minlength"){
								tip = tip.replace("{{minlength}}",attrs.minlength);
							}
							if(validationStr == "maxlength"){
								tip = tip.replace("{{maxlength}}",attrs.maxlength);
							}
						}

						scope.defaultValidation[optionName][validationStr] = tip;
					}
				});
			}
		};
	});

	diretiveModule.directive('formOptionText', function() {
		return {
			scope : true,
			restrict : "E",
			replace : true,
			require: '^plusDynamicForm',
			template : function(elm,attrs){
				var optionType = attrs.optionType?attrs.optionType:"text";

				//检查校验属性
				var validationStr = "";
				if(typeof attrs.required != "undefined"){
					validationStr += " required";
				}
				if(attrs.minlength){
					validationStr += ' ng-minlength="'+attrs.minlength+'"';
				}
				if(attrs.maxlength){
					validationStr += ' ng-maxlength="'+attrs.maxlength+'"';
				}
				if(attrs.pattern){
					validationStr += ' ng-pattern="'+attrs.pattern+'"';
				}

				return '<div class="form-group"> <label class="col-sm-3 control-label">'+attrs.optionLabel+'</label> ' +
						'<div class="col-sm-8"> <input autocomplete="false" type="'+optionType+'" name="'+attrs.optionName+'" ng-model="'+"searchConditions."+attrs.optionName+'" class="form-control" '+validationStr+' placeholder="'+attrs.optionTip+'"> </div> </div>';
			},
			link : function(scope, elm, attrs) {
				var optionName = attrs.optionName;
				scope.searchNames.push(optionName);
				scope.cleanNames.push(optionName);

				scope.formNames.push(optionName);
				scope.defaultValidation[optionName] = {};

				angular.forEach(scope.validStrs,function(validationStr,index){
					if(typeof attrs[validationStr] != undefined){
						var tip;
						if(scope.formValidation[optionName]){
							tip = scope.formValidation[optionName][validationStr];
						}else{
							tip = formTip.default[validationStr];
							if(validationStr == "minlength"){
								tip = tip.replace("{{minlength}}",attrs.minlength);
							}
							if(validationStr == "maxlength"){
								tip = tip.replace("{{maxlength}}",attrs.maxlength);
							}
						}

						scope.defaultValidation[optionName][validationStr] = tip;
					}
				});

				//是否加上了邮箱验证
				if(attrs.emailTip == 'true'){
					$(elm).find('input').mailAutoTip({
						inputWidth : 420,
						inputHeight : 33,
						onHide:function(value){
							scope.$apply(function(){
								//var nameObj = scope[scope.formName][optionName];
								//nameObj.$viewValue = value;
								scope.searchConditions[attrs.optionName] = value;
							});
						}
					});
				}
			}
		};
	});

	diretiveModule.directive('formOptionDate', function() {
		return {
			scope : true,
			restrict : "E",
			replace : true,
			require: '^plusDynamicForm',
			controller: function ($scope) {
				$scope.clearInput = function (optionName) {
					//$scope.searchConditions.conditionName = "";
					delete $scope.searchConditions[optionName];
				};
			},
			template : function(elm,attrs){
				var optionTip = attrs.optionTip ? attrs.optionTip : "";
				var optionNameSTmp = "'"+attrs.optionName+"'";

				return '<div class="form-group"><label class="col-sm-3 control-label">'+attrs.optionLabel+'</label> ' +
						'<div class="col-sm-9"> ' +
						'<div class="input-group input-append date"><input name="'+attrs.optionName+'" class="form-control" readonly ng-model="searchConditions.'+attrs.optionName+'" placeholder="'+optionTip+'"/>' +
						'<div class="input-group-addon add-on"><a href="javascript:;"  ng-click="clearInput('+optionNameSTmp+')" class="icon-remove">&times;</a></div>' +
						'</div>'+
							//'<input type="text" name="'+attrs.optionName+'" ng-model="'+"searchConditions."+attrs.optionName+'"  data-date-format="'+dateFormat+'" class="form-control" placeholder="'+attrs.optionTip+'" readonly> ' +
						'</div></div>';
			},
			link : function(scope, elm, attrs) {
				var optionName = attrs.optionName;

				scope.searchNames.push(optionName);
				scope.cleanNames.push(optionName);

				var format = attrs.dateFormat ? attrs.dateFormat : "yyyy-mm-dd hh:00";
				var defaultOptions = {
					format: format,
					todayBtn: true,
					todayHighlight: true,
					autoclose: true,
					language: "zh-CN",
					minView: "1"
				};

				if(attrs.dateOption){
					var cusOption = scope[attrs.dateOption];
					for(var key in cusOption){
						defaultOptions[key] = cusOption[key];
					}
				}

				$(elm).find("input").datetimepicker(defaultOptions).on("hide",function(){
					scope.searchConditions[attrs.optionName] = $(this).val();
				});

				scope.$watch("optionName", function (newVal, oldVal) {
					if (newVal !== oldVal) {
						scope.searchConditions[optionName] = newVal;
					}
				}, true);
			}
		};
	});

	diretiveModule.directive('formOptionSelect', function($injector) {
		return {
			scope : true,
			restrict : "E",
			replace : true,
			require: '^plusDynamicForm',
			controller : function($scope){
				$scope.conditionConf = {};
			},
			template: function(elm,attrs){
				var optionTip = attrs.optionTip?attrs.optionTip:"";
				var optionDisplay = 'option.'+attrs.selectValue;
				if(attrs.optionDisplay){
					optionDisplay = attrs.optionDisplay;
				}
				var selectValue = attrs.selectValue;
				var cutLength = attrs.cutLength;
				var strFilter = "";
				if(/^[0-9]*[1-9][0-9]*$/.test(cutLength)){
					strFilter = "|substring:"+cutLength;
				}

				var choiceHStr = "";
				if(attrs.choiceHeight){
					choiceHStr += "style='height:"+attrs.choiceHeight+";overflow-y:scroll;'";
				}

				var refreshStr = "";
				if(attrs.refresh && attrs.refresh=="true"){
					refreshStr = 'refresh="refreshFn($select.search)" refresh-delay="0"';
				}

				var template = '<div class="form-group">'+
						'<label class="col-sm-3 control-label">'+attrs.optionLabel+'</label>'+
						'<div class="col-sm-9">'+
						'<div>'+
						'<ui-select ng-model="conditionConf.optionName" style="width:172px;height:32px;">'+
						'<ui-select-match placeholder="'+optionTip+'" title="{{$select.selected.' + selectValue + '}}">{{$select.selected.'+attrs.selectValue+ strFilter +'}}</ui-select-match>'+
						'<ui-select-choices  '+refreshStr+'  repeat="option in options | filter: $select.search" '+choiceHStr+'>'+
						'<div ng-bind-html="'+optionDisplay+ strFilter +' | highlight: $select.search" title="{{' + optionDisplay + '}}"></div>'+
						'</ui-select-choices>'+
						'</ui-select>'+
						'</div>'+
						'</div>'+
						'</div>';

				return template;
			},
			compile : function(tElm,tAttrs){
				return {
					post : function(scope,elm,attrs){
						var optionName = attrs.optionName;
						var selectKey = attrs.selectKey;
						var needTipOption = attrs.needTipOption&&attrs.needTipOption=="true";
						var optionTip = attrs.optionTip;

						scope.options = [];

						scope.loadSelectionData = function(conditionObj){
							//根据最新的条件获取数据
							var condition = scope[attrs.optionCondition];

							if(condition==null || condition == undefined){
								if(conditionObj != null && conditionObj != undefined){
									condition = conditionObj;
								}else{
									condition = {};
								}
							}

							var DataService = $injector.get(attrs.serviceName);
							DataService[attrs.serviceMethod](condition).then(function(options){
								console.log("请求成功");
								console.log(options);

								if(!options){
									options = [];
								}

								// 插入提示选项
								if(needTipOption){
									var totalOption = {};
									totalOption.totalFlag = true;
									totalOption[attrs.selectValue] = optionTip;
									options.splice(0, 0, totalOption);
								}

								scope.options.length=0;
								for(var index in options){
									scope.options.push(options[index]);
								}

								if(options.length > 0){
									scope.conditionConf.optionName = options[0];
								}
							});
						};

						if(attrs.refresh && attrs.refresh=="true"){
							scope.refreshFn = function(searchStr){
								if(!searchStr) return;

								var conObj = {};
								conObj[optionName] = searchStr;
								scope.loadSelectionData(conObj);
							};
						}else{
							scope.loadSelectionData();
						}

						scope.$watch("conditionConf.optionName", function (newVal, oldVal) {
							if (newVal !== oldVal) {
								var backVal = newVal[selectKey];

								if (newVal.totalFlag) {
									delete scope.searchConditions[optionName];
								} else {
									scope.searchConditions[optionName] = backVal;
								}

								if(attrs.changeDo){
									scope[attrs.changeDo](newVal.totalFlag?null:newVal);
								}
							}
						}, true);

						//监听列表选项更新事件
						scope.$on("reload-"+optionName,function(event, conditionObj) {
							scope.loadSelectionData(conditionObj);
						});

						//清空监听
						scope.searchNames.push(optionName);
						scope.refreshNames.push(optionName);
						scope.$on("refresh-"+optionName,function(event,msg){
							if(!scope.options) return;

							scope.conditionConf.optionName = scope.options[0];
						});
						if(scope.echo){
							scope.$on("echo-"+optionName,function(event,selectV){
								if(!angular.isObject(selectV)) return;
								for(var i=0;i<scope.options.length;i++){
									if(scope.options[i][selectKey] == selectV[selectKey]){
										scope.conditionConf.optionName = scope.options[i];
										break;
									}
								}
							});
						}
					}
				};
			}
		};
	});

	diretiveModule.directive('formOptionSelectCascade', function($injector) {
		return {
			scope : true,
			restrict : "E",
			replace : true,
			require: '^plusDynamicForm',
			controller : function($scope){
				$scope.conditionConf = {};
			},
			template: function(elm,attrs){
				var selectValue = attrs.selectValue;
				var optionTip1 = attrs.optionTip1?attrs.optionTip1:"请选择";
				var optionTip2 = attrs.optionTip2?attrs.optionTip2:"请选择";
				var optionTip3 = attrs.optionTip3?attrs.optionTip3:"请选择";

				var cutLength = attrs.cutLength;
				var strFilter = "";
				if(/^[0-9]*[1-9][0-9]*$/.test(cutLength)){
					strFilter = "|substring:"+cutLength;
				}

				var template =
						'<div class="form-group">'+
						'<label class="col-sm-3 control-label">'+attrs.optionLabel+'</label>'+
						'<div class="col-sm-9">'+
						'<div>'+
						'<ui-select ng-disabled="isCascading" ng-model="vm.xxx1" on-select="xxx1select()" style="width:172px;height:32px;display:inline-block;">'+
						'<ui-select-match placeholder="'+optionTip1+'"  title="{{$select.selected.' + selectValue + '}}">{{$select.selected.'+selectValue + strFilter +'}}</ui-select-match>'+
						'<ui-select-choices repeat="option in options1">'+
						'<div ng-bind-html="option.'+selectValue + strFilter +' | highlight: $select.search" title="{{option.' + selectValue + '}}"></div>'+
						'</ui-select-choices>'+
						'</ui-select>'+
						'<ui-select ng-disabled="isCascading" ng-show="options2" on-select="xxx2select()" ng-model="vm.xxx2" style="width:172px;height:32px;display:inline-block;">'+
						'<ui-select-match placeholder="'+optionTip2+'" title="{{$select.selected.' + selectValue + '}}">{{$select.selected.'+selectValue + strFilter +'}}</ui-select-match>'+
						'<ui-select-choices repeat="option in options2">'+
						'<div ng-bind-html="option.'+selectValue + strFilter +' | highlight: $select.search" title="{{option.' + selectValue + '}}"></div>'+
						'</ui-select-choices>'+
						'</ui-select>'+
						'<ui-select ng-disabled="isCascading" ng-show="options3" on-select="xxx3select()" ng-model="vm.xxx3" style="width:172px;height:32px;display:inline-block;">'+
						'<ui-select-match placeholder="'+optionTip3+'" title="{{$select.selected.' + selectValue + '}}">{{$select.selected.'+selectValue + strFilter +'}}</ui-select-match>'+
						'<ui-select-choices repeat="option in options3">'+
						'<div ng-bind-html="option.'+selectValue + strFilter +' | highlight: $select.search" title="{{option.' + selectValue + '}}"></div>'+
						'</ui-select-choices>'+
						'</ui-select>'+
						'</div>'+
						'</div>'+
						'</div>';

				return template;
			},
			compile : function(tElm,tAttrs){
				return {
					post : function(scope,elm,attrs){
						var optionName = attrs.optionName;
						var selectKey = attrs.selectKey;
						var selectValue = attrs.selectValue;
						var selectChildren = attrs.selectChildren;
						var noneTip = "== 请选择 ==";
						var selectedOption = {};

						function initSelectData(){
							scope.vm = { "xxx1": scope.options1[0]};

							scope.vm.xxx2 = null;
							scope.options2 = null;
							scope.vm.xxx3 = null;
							scope.options3 = null;

							var children2 = scope.options1[0][selectChildren];
							if(children2 && children2.length > 0){

								if(children2[0][selectKey] != -1){
									// 插入全部选项
									var tipOption = {};
									tipOption[selectKey] = -1;
									tipOption[selectValue] = noneTip;
									children2.splice(0, 0, tipOption);
								}

								scope.options2 = children2;
								scope.vm.xxx2 = children2[0];
							}

							var tmpXXX1 = angular.copy(scope.vm.xxx1);
							tmpXXX1[selectChildren] = null;
							scope.searchConditions[optionName] = selectedOption = tmpXXX1;
						}

						scope.loadSelectionData = function(){
							scope.isCascading = true;

							var DataService = $injector.get(attrs.serviceName);
							DataService[attrs.serviceMethod]().then(function(options){
								console.log("请求成功");
								console.log(options);

								scope.options1 = options;

								initSelectData();
								scope.isCascading = false;
							});
						};

						scope.loadSelectionData();

						scope.xxx1select = function(){
							scope.isCascading = true;

							var tmpXXX1 = angular.copy(scope.vm.xxx1);
							tmpXXX1[selectChildren] = null;
							scope.searchConditions[optionName] = selectedOption = tmpXXX1;

							scope.options2 = scope.vm.xxx1[selectChildren];

							//无子节点
							if(!scope.options2 || scope.options2.length==0){
								scope.vm.xxx2 = null;
								scope.options3 = null;
								scope.vm.xxx3 = null;

								selectedOption[selectChildren] = null;
								scope.isCascading = false;
								return;
							}

							//请选择
							if(scope.options2[0][selectKey] == -1){
								scope.vm.xxx2 = scope.options2[0];
								scope.options3 = null;
								scope.vm.xxx3 = null;

								selectedOption[selectChildren] = null;
								scope.isCascading = false;
								return;
							}

							//有子节点，且第一次选择
							// 插入全部选项
							var tipOption = {};
							tipOption[selectKey] = -1;
							tipOption[selectValue] = noneTip;
							scope.options2.splice(0, 0, tipOption);
							scope.vm.xxx3 = tipOption;
							scope.options3 = null;
							scope.vm.xxx3 = null;
							scope.isCascading = false;
						};

						scope.xxx2select = function(){
							scope.isCascading = true;

							var newVal = scope.vm.xxx2;
							if(newVal[selectKey] == -1){
								scope.options3 = null;
								scope.vm.xxx3 = null;

								selectedOption[selectChildren] = null;
								scope.isCascading = false;
								return;
							}

							if(selectedOption[selectChildren]){
								selectedOption[selectChildren].length = 0;
							}else{
								selectedOption[selectChildren] = [];
							}
							var tmpValue = angular.copy(newVal);
							tmpValue[selectChildren] = null;
							selectedOption[selectChildren].push(tmpValue);

							scope.options3 = newVal[selectChildren];
							//无子节点
							if(!scope.options3 || scope.options3.length==0){
								scope.vm.xxx3 = null;
								scope.isCascading = false;
								return;
							}

							//请选择
							if(scope.options3[0][selectKey] == -1){
								scope.vm.xxx3 = scope.options3[0];
								scope.isCascading = false;
								return;
							}

							//有子节点，且第一次选择
							// 插入全部选项
							var tipOption = {};
							tipOption[selectKey] = -1;
							tipOption[selectValue] = noneTip;
							scope.options3.splice(0, 0, tipOption);
							scope.vm.xxx3 = tipOption;
							scope.isCascading = false;
						};

						scope.xxx3select = function(){
							scope.isCascading = true;

							var newVal = scope.vm.xxx3;
							var xxx2 = selectedOption[selectChildren][0];
							if(newVal[selectKey] == -1){
								xxx2[selectChildren] = null;

								scope.isCascading = false;
								return;
							}

							if(xxx2[selectChildren]){
								xxx2[selectChildren].length = 0;
							}else{
								xxx2[selectChildren] = [];
							}
							var tmpValue = angular.copy(newVal);
							tmpValue[selectChildren] = null;
							xxx2[selectChildren].push(tmpValue);

							scope.isCascading = false;
						};

						//清空监听
						scope.searchNames.push(optionName);
						scope.refreshNames.push(optionName);
						scope.$on("refresh-"+optionName,function(event,msg){
							scope.isCascading = true;
							initSelectData();
							scope.isCascading = false;
						});
						if(scope.echo){
							scope.$on("echo-"+optionName,function(event,ssOption){
								if(!angular.isObject(ssOption)) return;

								//scope.isCascading = true;
								var tmpxxx1,tmpxxx2,tmpxxx3,tmpopts2,tmpopts3,flag;
								for(var i1 in scope.options1){
									tmpxxx1 = scope.options1[i1];
									if(tmpxxx1[selectKey] == ssOption[selectKey]){
										flag = true;
										tmpxxx2 = null;
										tmpxxx3 = null;
										break;
									}

									tmpopts2 = tmpxxx1[selectChildren];
									for(var i2 in tmpopts2){
										tmpxxx2 = tmpopts2[i2];
										if(tmpxxx2[selectKey] == ssOption[selectKey]){
											flag = true;
											tmpxxx3 = null;
											break;
										}

										tmpopts3 = tmpxxx2[selectChildren];
										for(var i3 in tmpopts3){
											tmpxxx3 = tmpopts3[i3];
											if(tmpxxx3[selectKey] == ssOption[selectKey]){
												flag = true;
												break;
											}
										}
										if(flag) break;

									}
									if(flag) break;

								}

								var tmpXXX1 = angular.copy(scope.vm.xxx1);
								tmpXXX1[selectChildren] = null;
								scope.searchConditions[optionName] = selectedOption = tmpXXX1;

								if(tmpxxx3){
									scope.options2 = tmpopts2;
									scope.options3 = tmpopts3;

									scope.vm.xxx1 = tmpxxx1;
									scope.vm.xxx2 = tmpxxx2;
									scope.vm.xxx3 = tmpxxx3;

									selectedOption[selectChildren].length = 0;

									var ttt = angular.copy(tmpxxx2);
									ttt[selectChildren].length = 0;

									selectedOption[selectChildren].push(ttt);

									scope.xxx3select();
									return;
								}

								if(tmpxxx2){
									scope.options2 = tmpopts2;

									scope.vm.xxx1 = tmpxxx1;
									scope.vm.xxx2 = tmpxxx2;

									selectedOption[selectChildren].length = 0;

									scope.xxx2select();
									return;
								}

								scope.vm.xxx1 = tmpxxx1;
								scope.xxx1select();

							});
						}

					}
				};
			}
		};
	});

	diretiveModule.directive('formOptionCheckbox', function($injector) {
		return {
			scope : true,
			restrict : "E",
			replace : true,
			require: '^plusDynamicForm',
			controller : function($scope){
				$scope.conditionConf = {};
			},
			template: function(elm,attrs){
				var template = '<div class="form-group">'+
						'<label class="col-sm-3 control-label">'+attrs.optionLabel+'</label>'+
						'<div class="col-sm-9">'+
						'<div class="checkbox">'+
						'<label ng-repeat="option in options"><input name="'+attrs.optionName+'" type="checkbox"  ng-model="options[$index].ck" ng-click="checkOpt(option)">{{option.'+attrs.selectValue+'}}&nbsp;&nbsp;&nbsp;</label>'+
						'</div>'+
						'</div>'+
						'</div>';

				return template;
			},
			compile : function(tElm,tAttrs){
				return {
					post : function(scope,elm,attrs) {
						var optionName = attrs.optionName;
						var selectKey = attrs.selectKey;
						var selectValue = attrs.selectValue;
						var originalOptions = [];

						scope.checkOpt = function (option) {
							option.ck = !option.ck;

							//判断是否全部
							if(option[selectKey] == -1){
								if(option.ck){
									scope.searchConditions[optionName] = originalOptions;
									for(var i=0;i<scope.options.length;i++){
										scope.options[i].ck = true;
									}
								}else{
									delete scope.searchConditions[optionName];
									for(var j=0;j<scope.options.length;j++){
										scope.options[j].ck = false;
									}
								}

								return;
							}

							//非全部
							var tmpOptions = [];
							for(var k=0;k<scope.options.length;k++){
								var opt = scope.options[k];
								if(opt.ck && opt[selectKey]!=-1){
									var tmpO = angular.copy(opt);
									delete tmpO.ck;
									tmpOptions.push(tmpO);
								}
							}
							scope.searchConditions[optionName] = tmpOptions;

							//设置全部选项
							if(tmpOptions.length == scope.options.length-1){
								scope.options[0].ck = true;
							}else{
								scope.options[0].ck = false;
							}
						};


						scope.loadSelectionData = function () {
							var DataService = $injector.get(attrs.serviceName);
							DataService[attrs.serviceMethod]().then(function (options) {
								console.log("请求成功");
								console.log(options);

								originalOptions = angular.copy(options);

								// 插入全部选项
								var totalOption = {};
								totalOption[selectKey] = -1;
								totalOption[selectValue] = "全部";
								options.splice(0,0,totalOption);

								for(var index in options){
									options[index].ck = false;
								}
								scope.options = options;
							});
						};

						scope.loadSelectionData();

						//清空监听
						scope.searchNames.push(optionName);
						scope.refreshNames.push(optionName);

						scope.$on("refresh-"+optionName,function(event,msg){
							delete scope.searchConditions[optionName];
							for(var i=0;i<scope.options.length;i++){
								scope.options[i].ck = false;
							}
						});
						if(scope.echo){
							scope.$on("echo-"+optionName,function(event,echoOpts){
								if(!echoOpts || echoOpts.length==0) return;

								for(var i=0;i<scope.options.length;i++){
									scope.options[i].ck = false;
								}

								var echoCount = 0;//回显的项数
								for(var i=0;i<echoOpts.length;i++){
									var echoOpt = echoOpts[i];
									for(var j=0;j<scope.options.length;j++){
										if(scope.options[j][selectKey] === echoOpt[selectKey]){
											scope.options[j].ck = true;
											echoCount++;
											break;
										}
									}
								}

								if(echoCount == scope.options.length-1){
									scope.options[0].ck = true;
								}
							});
						}
					}
				}
			}
		};
	});

	diretiveModule.directive('formOptionRadio', function($injector) {
		return {
			scope : true,
			restrict : "E",
			replace : true,
			require: '^plusDynamicForm',
			controller : function($scope){
				$scope.conditionConf = {};
//				$scope.selectedIndex = 0;//默认第一个被选中
			},
			template: function(elm,attrs){
				var template = '<div class="form-group">'+
						'<label class="col-sm-3 control-label">'+attrs.optionLabel+'</label>'+
						'<div class="col-sm-9">'+
						'<div class="radio">'+
						'<label ng-repeat="option in options" ng-click="checkOpt($index)">' +
						'<input name="'+attrs.optionName+'" type="radio" ng-checked="$index==selectedIndex">{{option.'+attrs.selectValue+'}}&nbsp;&nbsp;&nbsp;</label>'+
						'</div>'+
						'</div>'+
						'</div>';

				return template;
			},
			compile : function(tElm,tAttrs){
				return {
					post : function(scope,elm,attrs) {
						var optionName = attrs.optionName;
						var selectKey = attrs.selectKey;
						var selectValue = attrs.selectValue;

						/**
						 * 吕鹏增加 默认选项
						 */
						scope.selectedIndex = 0;
						if(attrs.selectedIndex){
							scope.selectedIndex = attrs.selectedIndex;//默认第一个被选中
						}

						scope.checkOpt = function (clickIndex) {
							scope.selectedIndex = clickIndex;
							scope.searchConditions[optionName] = scope.options[clickIndex][selectKey];

							if(attrs.checkDo){
								scope[attrs.checkDo](clickIndex,scope.options[clickIndex],scope.options);
							}
						};

						scope.loadSelectionData = function () {
							var DataService = $injector.get(attrs.serviceName);
							DataService[attrs.serviceMethod]().then(function (options) {
								console.log("请求成功");
								console.log(options);

								scope.options = options;
								scope.searchConditions[optionName] = scope.options[0][selectKey];
							});
						};

						scope.loadSelectionData();

						//清空监听
						scope.searchNames.push(optionName);
						scope.refreshNames.push(optionName);

						scope.$on("refresh-"+optionName,function(event,msg){
							scope.searchConditions[optionName] = scope.options[0][selectKey];
							scope.selectedIndex = 0;
							if(attrs.selectedIndex){
								scope.selectedIndex = attrs.selectedIndex;//默认第一个被选中
							}
						});
						if(scope.echo){
							scope.$on("echo-"+optionName,function(event,echoOpt){
								if(!echoOpt) return;

								for(var i=0;i<scope.options.length;i++) {
									if (echoOpt[selectKey] == scope.options[i][selectKey]) {
										scope.searchConditions[optionName] = scope.options[i][selectKey];
										scope.selectedIndex = i;
										break;
									}
								}

							});
						}
					}
				}
			}
		};
	});

	diretiveModule.directive('formOptionTree', function($injector) {

		return {
			scope : true,
			restrict : "E",
			replace : true,
			require: '^plusDynamicForm',
			controller : function($scope){
				$scope.conditionConf = {};
			},
			template: function(elm,attrs){
				var selectKeyS = attrs.selectKey;
				var selectValueS = attrs.selectValue;
				var selectChildrenS = attrs.selectChildren;

				var template =
						'<div class="form-group">'+
						'<label class="col-sm-3 control-label">'+attrs.optionLabel+'</label>'+
						'<div class="col-sm-9">'+

						'<ol class="nestable dd-list list-unstyled" style="background:#f2f4f8;color:#333;">'+
						'<li class="dd-item" ng-repeat="entity in options">'+
						'<span class="btn btn-default btn-xs dd-handle pull-left"><input type="checkbox" ng-model="entity.ck" ng-click="lv1Changed(entity)" /></span>'+
						'<button ng-if="entity.'+selectChildrenS+'" class="btn btn-default dd-toggle collapsed" data-toggle="collapse" data-target="#fot{{entity.'+selectKeyS+'}}"></button>'+
						'<div class="dd-content"><span>{{entity.'+selectValueS+'}}</span></div>'+

						'<ol ng-if="entity.'+selectChildrenS+'" class="dd-list collapse" id="fot{{entity.'+selectKeyS+'}}" style="height:0;">'+
						'<li class="dd-item" ng-repeat="entity2 in entity.'+selectChildrenS+'">'+
						'<span class="btn btn-default btn-xs dd-handle pull-left"><input type="checkbox" ng-model="entity2.ck" ng-click="lv2Changed(entity,entity2)" /></span>'+
						'<button ng-if="entity2.'+selectChildrenS+'" class="btn btn-default dd-toggle collapsed" data-toggle="collapse" data-target="#fot{{entity2.'+selectKeyS+'}}"></button>'+
						'<div class="dd-content"><span>{{entity2.'+selectValueS+'}}</span></div>'+

						'<ol ng-if="entity2.'+selectChildrenS+'" class="dd-list collapse" id="fot{{entity2.'+selectKeyS+'}}" style="height:0;">'+
						'<li class="dd-item" ng-repeat="entity3 in entity2.'+selectChildrenS+'">'+
						'<span class="btn btn-default btn-xs dd-handle pull-left"><input type="checkbox" ng-model="entity3.ck" ng-click="lv3Changed(entity,entity2,entity3)"/></span>'+
						'<div class="dd-content"><span>{{entity3.'+selectValueS+'}}</span></div>'+
						'</li>'+
						'</ol>'+
						'</li>'+
						'</ol>'+
						'</li>'+
						'</ol>'+

						'</div>'+
						'</div>';

				return template;
			},
			compile : function(tElm,tAttrs){
				return {
					post : function(scope,elm,attrs) {
						var optionName = attrs.optionName;
						var selectKey = attrs.selectKey;
						var selectValue = attrs.selectValue;
						var selectChildren = attrs.selectChildren;
						var originalOptions = [];

						scope.loadSelectionData = function () {
							var DataService = $injector.get(attrs.serviceName);
							DataService[attrs.serviceMethod]().then(function (options) {
								originalOptions = angular.copy(options);

								scope.searchConditions[optionName] = options;
								scope.options = options;
							});
						};

						scope.loadSelectionData();

						scope.lv1Changed = function(en1){
							var ckstate = !en1.ck;
							en1.ck = ckstate;

							// 自动设置所有下级
							var children1 = en1[selectChildren];
							if(children1){
								for(var i1 in children1){
									var en2 = children1[i1];
									en2.ck = ckstate;

									var children2 = en2[selectChildren];
									if(children2){
										for(var i2 in children2){
											var en3 = children2[i2];
											en3.ck = ckstate;
										}
									}
								}
							}
						};

						scope.lv2Changed = function(en1,en2){
							var ckstate = !en2.ck;
							en2.ck = ckstate;


							// 自动设置所有下级
							var children2 = en2[selectChildren];
							if(children2){
								for(var i2 in children2){
									var en3 = children2[i2];
									en3.ck = ckstate;
								}
							}

							//设置1级
							if(ckstate){
								en1.ck = true;
							}else{
								var children1 = en1[selectChildren];
								var needChange = true;
								for(var i1 in children1){
									if(children1[i1].ck){
										needChange = false;
										break;
									}
								}
								if(needChange){
									en1.ck = false;
								}
							}

						}

						scope.lv3Changed = function(en1,en2,en3){
							var ckstate = !en3.ck;
							en3.ck = ckstate;

							//设置2级
							if(ckstate){
								en2.ck = true;
							}else{
								var children2 = en2[selectChildren];
								var needChange2 = true;
								for(var i2 in children2){
									if(children2[i2].ck){
										needChange2 = false;
										break;
									}
								}
								if(needChange2){
									en2.ck = false;
								}
							}

							//设置1级
							if(ckstate){
								en1.ck = true;
							}else{
								var children1 = en1[selectChildren];
								var needChange = true;
								for(var i1 in children1){
									if(children1[i1].ck){
										needChange = false;
										break;
									}
								}
								if(needChange){
									en1.ck = false;
								}
							}

						}

						//scope.searchNames.push(optionName);
						scope.refreshNames.push(optionName);

						//清空
						scope.$on("refresh-"+optionName,function(event,msg){
							for(var i=0;i<scope.options.length;i++){
								scope.options[i].ck = true;
								scope.lv1Changed(scope.options[i]);
							}
						});
						if(scope.echo){
							scope.$on("echo-"+optionName,function(event,echoOpts){
								if(!echoOpts || echoOpts.length==0) return;

								//清空
								for(var i=0;i<scope.options.length;i++){
									scope.options[i].ck = true;
									scope.lv1Changed(scope.options[i]);
								}

								//回显
								for(var i=0;i<echoOpts.length;i++){
									var optt = echoOpts[i];
									echoOption(scope.options,optt);
								}
							});
						}

						function echoOption(totalOptions,selectOpt){
							var sOpt;
							for(var j=0;j<totalOptions.length;j++){
								if(totalOptions[j][selectKey] === selectOpt[selectKey]){
									sOpt = totalOptions[j];
									sOpt.ck = true;
									break;
								}
							}

							if(!selectOpt[selectChildren]){
								return;
							}

							for(var tmpI in selectOpt[selectChildren]){
								echoOption(sOpt[selectChildren],selectOpt[selectChildren][tmpI]);
							}
						}
					}
				}
			}
		};
	});
});